package org.jeecg.modules.demo.yy.service.impl;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.java_websocket.WebSocket;
import org.jeecg.modules.demo.yy.Model.BaiduWenxinYiYanToken;
import org.jeecg.modules.demo.yy.entity.YyhzQa;
import org.jeecg.modules.demo.yy.mapper.YyhzQaMapper;
import org.jeecg.modules.demo.yy.service.IYyhzQaService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.io.IOException;

/**
 * @Description: 语音盒子QA
 * @Author: jeecg-boot
 * @Date:   2024-06-14
 * @Version: V1.0
 */
@Service
public class YyhzQaServiceImpl extends ServiceImpl<YyhzQaMapper, YyhzQa> implements IYyhzQaService {


    @Override
    public String webwenxinyiyanApi(String message) throws IOException {
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        String token= BaiduWenxinYiYanToken.getWenxinToken();
        String access_token = token;
        // 大模型api
        HttpPost post = new HttpPost("https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/ernie-tiny-8k?access_token=" + access_token);
        post.addHeader("Content-Type", "application/json");
        post.addHeader("Accept", "application/json");
        // 问题内容。
        String messageContent = message;
        String paramJson = "{\"messages\": [{\"role\": \"user\", \"content\": \"" + messageContent + "\"}]}\n";
        StringEntity stringEntity = new StringEntity(paramJson,
                ContentType.create("application/json", "UTF-8"));
        post.setEntity(stringEntity);
        CloseableHttpResponse response = httpClient.execute(post);
        String s = EntityUtils.toString(response.getEntity());
        return s;
    }


}
